<?php
/**
 * view blade 全局数据
 * Author: Zeon
 * Date: 2018/1/27
 * Created by Panxsoft.
 */
namespace App\Presenters;

use App\Http\Models\Admin\AboutUs;
use App\Http\Models\Admin\ProductCategory;
use App\Http\Models\ArticleTag;
use App\Http\Models\VideoType;
use Illuminate\Database\Eloquent\Collection;

class GlobalViewPresenter
{
    public static $about_us = null;

    public static $project_cases = null;

    /**
     * @return array|\Illuminate\Database\Eloquent\Collection|static[]
     */
    public static function productCategories()
    {
        $prod_cats = (new ProductCategory())
            ->filterTopLevel()
            ->with('bladeNavSubCategories')
            ->latest('priority')
            ->limit(5)
            ->get();

        $prod_cats->transform(function (ProductCategory $category) {
            $category->is_more = count($category->bladeNavSubCategories)>10 ? true : false;
            $category->sub_categories = $category->bladeNavSubCategories->take(10);
            $category->addHidden(['bladeSubCategories']);
            return $category;
        });

        return $prod_cats;
    }

    /**
     * @return null|Collection
     */
    public static function getAboutUs()
    {
        if (is_null(self::$about_us)) {
            self::$about_us = (new AboutUs)->latest()->first();
        }

        return self::$about_us;
    }

    /**
     * @deprecated  不用下拉了
     *
     * @return null|array
     */
    public static function getProjectCases()
    {
        if (is_null(self::$project_cases)) {
            self::$project_cases = (new ArticleTag)->where('super_tag_id', ArticleTag::PROJECT_CASE)
                ->latest('sort')
                ->limit(5)
                ->pluck('name', 'id')
                ->all();
        }

        return self::$project_cases;
    }

    /**
     *  返回页脚的数据信息
     * @return array
     */
    public static function getFooterData()
    {
        // 产品中心
        $product_types = ProductCategory::where('super_id', ProductCategory::TOP_CATEGORY)
            ->latest('priority')
            ->limit(5)
            ->with('getSubCategories')
            ->get();

        $product_types = self::transformProductCategories($product_types);

        $article_types = ArticleTag::where('super_tag_id', ArticleTag::TOP_TAG)
            ->latest('sort')
            ->with('getSubTags')
            ->get();

        $video_types = VideoType::limit(5)->pluck('name', 'id')->all();

        return [
            '产品中心' => $product_types,
            '解决方案' => self::getArticleTypes($article_types, ArticleTag::SOLUTION_CASE),
            '工程案例' => self::getArticleTypes($article_types, ArticleTag::PROJECT_CASE),
            '视频专区' => $video_types,
            '企业动态' => self::getArticleTypes($article_types, ArticleTag::COMPANY_NEWS),
        ];
    }

    /**
     * @param Collection $product_categories
     * @return Collection
     */
    private static function transformProductCategories(Collection $product_categories)
    {
        return $product_categories->transform(function(ProductCategory $product_category){
            if (count($product_category->getSubCategories)) {
                $product_category->id = $product_category->getSubCategories->first()->id;
            }
            $product_category->addHidden('getSubCategories');

            return $product_category;
        });
    }

    /**
     * @param $article_types
     * @param $id
     * @return mixed
     */
    private static function getArticleTypes($article_types, $id)
    {
        $new_article_type = $article_types->where('id', $id)->first();

        return $new_article_type->getSubTags->take(5)->pluck('name', 'id')->all();

    }

}
